package com.gopay.common.user.dao.impl;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.domain.User4Amount;
import com.gopay.common.user.dao.UserDailyBalStatsDAO;
import com.gopay.common.util.DateUtils;
import com.gopay.common.util.StringTools;

/**
 * 
 * @UserDailyBalStatsDAOImpl.java
 * @author 曹永峰
 */
@Repository("userDailyBalStatsDao")
public class UserDailyBalStatsDAOImpl extends
		BaseDAOImpl<User4Amount, Serializable> implements UserDailyBalStatsDAO {

	private static final String sql = "select a.ACCT_ID,a.ava_amt,sum(f.fre_amt)," +
			"sum(case when t.dr_cr ='D' then t.tran_amt else 0 end),sum(case when " +
			"t.dr_cr ='C' then t.tran_amt else 0 end),sum(case when t.dr_cr ='D' " +
			"then 1 else 0 end),sum(case when t.dr_cr ='C' then 1 else 0 end)from " +
			"ACPS_ACCT_MAST a left join ACPS_FREZ_TRANS_DTL f on  a.ACCT_ID = " +
			"f.ACCT_ID and f.FRE_TIME >= to_date(:tdstart,'yy/MM/dd HH24:mi:ss') " +
			" and f.FRE_TIME < to_date(:tdend,'yy/MM/dd HH24:mi:ss') left join " +
			"ACPS_ACCT_TRANS_DTL t on  a.ACCT_ID = t.ACCT_ID and " +
			" t.TRAN_TIME >= to_date(:tdstart,'yy/MM/dd HH24:mi:ss') " +
			" and t.TRAN_TIME < to_date(:tdend,'yy/MM/dd HH24:mi:ss') group by a.ACCT_ID,a.ava_amt";

	/**
	 * 统计日终余额信息
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public List<User4Amount> getBalInfo(String date,int index,int length) {
		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("today", date);
		map.put("tdstart", date +"  00:00:00");
		map.put("tdend", date +" 23:59:59");
		List list = findBySql(sql, map,index,length);
		List<User4Amount> user4Amounts = new ArrayList<User4Amount>();
		AtomicInteger sq = new AtomicInteger(1);
		for (Object obj : list) {
			Object[] o = (Object[]) obj;

			User4Amount user4Amount = new User4Amount();
			user4Amount.setAcctId(StringTools.checkNulltoString(o[0]));
			user4Amount.setStlmDate(DateUtils.now2string("yyyyMMdd"));
			user4Amount.setDayCurBal(checkNull(o[1]));
			user4Amount.setDayLockAmt(checkNull(o[2]));
			user4Amount.setTotalDebitAmt(checkNull(o[3]));
			user4Amount.setTotalCreditAmt(checkNull(o[4]));
			user4Amount.setDayDebitCount(((BigDecimal) o[5]).intValue());
			user4Amount.setDayCreditCount(((BigDecimal) o[6]).intValue());
			user4Amount
					.setSq(System.currentTimeMillis() + sq.getAndIncrement());
			user4Amount.setCreatedTs(new Date());
			user4Amount.setLastUpdTs(user4Amount.getCreatedTs());
			user4Amounts.add(user4Amount);
		}
		return user4Amounts;
	}

	private BigDecimal checkNull(Object obj) {
		BigDecimal amt = new BigDecimal("0");
		if (obj == null) {
			return amt;
		}
		return new BigDecimal(obj.toString());
	}

	@Override
	public User4Amount getUser4Amount(String acctId, String stlmDate) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("acctId", acctId);
		map.put("stlmDate", stlmDate);
		List<User4Amount> user4Amount = findByHql(
				"from User4Amount where acctId=:acctId and stlmDate=:stlmDate",
				map);
		return user4Amount == null || user4Amount.size() == 0 ? null
				: user4Amount.get(0);
	}

	@Override
	public int getCount() {
		Map<String, Object> map = new HashMap<String, Object>();
		String sql ="select  count(a.ACCT_ID) from ACPS_ACCT_MAST a ";
		int count = findCountBySql(sql, map);
		return count;
	}
	@Override
	public List<Object> getAccts(int index,int length) {
		Map<String, Object> map = new HashMap<String, Object>();
		String sql ="select a.ACCT_ID from ACPS_ACCT_MAST a ";
		List<?> result = findBySql(sql, map,index,length);
		return (List<Object>) result;
	}
}
